<?php 

if (! defined('BASEPATH')) exit('No direct script access');

class schedule_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_by_id($schedule_id = FALSE)
    {
        if ($schedule_id === FALSE)
        {
            return FALSE;
        }
        
        $query = $this->db->where('id', $schedule_id)
                          ->get('schedule');
        if ($query->num_rows() == 1)
        {
            return $query->row();
        }
        else
        {
            return FALSE;
        }
    }
    
    function get_all($num = FALSE, $offset = FALSE) 
    {
        $query = $this->db->select('*,(SELECT title FROM movie WHERE id = schedule.movie_id) movie_name,
                                    (SELECT name FROM cinema WHERE id = schedule.cinema_id) cinema_name,
                                    (SELECT branch.name FROM cinema INNER JOIN branch on cinema.branch_id=branch.id WHERE cinema.id = schedule.cinema_id) branch_name' )
                          ->order_by('start_time','DESC')
                          ->get('schedule', $num, $offset);
        return $query->result();
    }
    
    function get_by_movie_id_and_branch_id($movie_id,$branch_id,$num = FALSE, $offset = FALSE) 
    {
        $query = $this->db->select('schedule.*' )
                          ->join('cinema', 'cinema.id = schedule.cinema_id')
                          ->where('schedule.movie_id', $movie_id)
                          ->where('cinema.branch_id', $branch_id)
                          ->where('schedule.start_time > NOW() ')
                          ->get('schedule', $num, $offset);
        return $query->result();
    }
    
    
    
    function create($cinema_id = FALSE, $movie_id = FALSE, $start_time = FALSE , $end_time = FALSE)
    {
        if ($cinema_id === FALSE OR $movie_id === FALSE OR $start_time === FALSE OR $end_time === FALSE )
        {
            return FALSE;
        }
        if ($this->is_exist($start_time ,$end_time ,$cinema_id))
        {
            return FALSE;
        }

        $schedule = array('cinema_id' => $cinema_id,
                         'movie_id' => $movie_id,
                         'start_time' => $start_time,
                         'end_time' => $end_time
                        );
        $this->db->insert('schedule', $schedule);
	    return TRUE;
        
    }
    
    function update($schedule_id = FAlSE , $cinema_id = FALSE, $movie_id = FALSE, $start_time = FALSE , $end_time = FALSE)
    {
        if ($schedule_id === FALSE OR $cinema_id === FALSE OR $movie_id === FALSE OR $start_time === FALSE OR $end_time === FALSE )
        {
            return FALSE;
        }
        if ($this->is_exist($start_time ,$end_time ,$cinema_id ,$schedule_id))
        {
            return FALSE;
        }
        $schedule = array('cinema_id' => $cinema_id,
                         'movie_id' => $movie_id,
                         'start_time' => $start_time,
                         'end_time' => $end_time
                        );
        $this->db->where('id', $schedule_id);
        $this->db->update('schedule', $schedule);
        return TRUE;
    }
    
    function delete($schedule_id = FALSE)
    {
        if ($schedule_id === FALSE)
        {
            return FALSE;
        }
        $this->db->where('id', $schedule_id);
        $this->db->delete('schedule');
        return TRUE;
    }
    
    function is_exist($start_time ,$end_time ,$cinema_id , $schedule_id = FALSE)
    {
        $where = "( (start_time <= '$start_time' AND '$start_time' <= end_time) OR ( start_time <= '$end_time' AND '$end_time' <= end_time)) AND cinema_id = $cinema_id";
        $query = $this->db->where($where)
                          ->get('schedule');
        if($query->num_rows() > 1)
        {
            return TRUE;
        }                  
        else if ($query->num_rows() == 1)
        {
            $row = $query->row(); 
            return ($schedule_id == $row->id)?FALSE:TRUE;
        }
        else
        {
            return FALSE;
        }
    }
   
}
/* End of file fixture_model.php */
/* Location: ./system/application/models/fixture_model.php */